package edu.mit.csail.sdg.alloy4whole;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;

import kodkod.analysis.Problem;
import kodkod.analysis.ast.wGraph;
import kodkod.analysis.ordering.DefaultOrdering;
import edu.mit.csail.sdg.alloy4.A4Reporter;
import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4compiler.ast.Command;
import edu.mit.csail.sdg.alloy4compiler.ast.Module;
import edu.mit.csail.sdg.alloy4compiler.parser.CompUtil;

public class OutputAST {

	public static void main(String[] args) throws Err, FileNotFoundException {
		File input, output;
		
		if (args.length < 3) {
			System.out.println("Usage: OutputAST alloy-model.als command target.dot");
			System.exit(0);
		}
		
		input = new File(args[0]);
		output = new File(args[2]);
		
		if (!input.exists()) {
			System.out.println("Input " + args[0] + " not found");
			System.exit(0);
		}		
		if (output.exists()) {
			System.out.println("Output " + args[2] + " already exists");
			System.exit(0);
		}
		
		Module module = CompUtil.parseEverything_fromFile(A4Reporter.NOP, null, input.getAbsolutePath());
		
    	for (Command command: module.getAllCommands()) if (command.toString().contains(args[1])) {
			Problem problem = new Problem(module, command, new DefaultOrdering());
			System.out.println(problem);
			wGraph graph = new wGraph(problem);
			graph.printGraph(new PrintWriter(output));
		}
	}

}
